package com.moweilin.mo_gateway.handlers;

import com.moweilin.mo_gateway.services.IModuleService;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;

/**
 * Created by moweilin on 2017/2/28.
 */
public class ModuleRequest {
    private static final String PATH_SPLITTER = "/";
    private String originPath;
    private String moduleName;
    private String requestURI;
    private HttpMethod method;
    private IModuleService moduleService;
    private Module module;
    private HttpServerRequest request;
    public String getModuleName() {
        return moduleName;
    }

    public String getRequestURI() {
        return requestURI;
    }

    public HttpMethod getMethod() {
        return method;
    }

    public String getOriginPath() {
        return originPath;
    }

    public ModuleRequest(IModuleService moduleService, HttpServerRequest request) {
        this.request = request;
        this.moduleService = moduleService;
        this.originPath = request.path();
        this.method = request.method();
        String path = originPath;
        if (path.startsWith(PATH_SPLITTER)) {
            path = path.substring(1);
        }
        int splitIndex = path.indexOf(PATH_SPLITTER);
        if (splitIndex >= 0) {
            moduleName = path.substring(0, splitIndex);
            requestURI = path.substring(splitIndex + 1);
        } else {
            moduleName = null;
            requestURI = path;
        }
        module = moduleService.get(moduleName);
    }

    public Module getModule() {
        return module;
    }

    public HttpServerRequest getRequest() {
        return request;
    }
}
